package com.css.fxfzcpxx.modules.informationDelivery.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzcpxx.base.log.Log;
import com.css.fxfzcpxx.base.response.RestResponse;

import com.css.fxfzcpxx.modules.informationDelivery.entity.InfDelivery;
import com.css.fxfzcpxx.modules.informationDelivery.param.ClaCasParams;
import com.css.fxfzcpxx.modules.informationDelivery.param.QueParams;
import com.css.fxfzcpxx.modules.informationDelivery.service.InfDeliveryService;
import com.css.fxfzcpxx.modules.informationDelivery.vo.InfDeliveryVo;
import com.css.fxfzcpxx.modules.informationDelivery.vo.ListVo;
import com.css.fxfzcpxx.modules.websocket.WebSocketManager;
import com.css.fxfzcpxx.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @program
 * @description 知识学习
 * @create 2022/8/8 10:39
 **/
@RestController
@RequestMapping("/infDelivery")
@Slf4j
public class InfDeliveryController {
    @Resource
    InfDeliveryService infDeliveryService;

    private final WebSocketManager webSocketManager;

    @Autowired
    public InfDeliveryController(WebSocketManager webSocketManager) {
        this.webSocketManager = webSocketManager;
    }
    /***
     * 分页查询
     * @param queParams
     * @return
     */
    @GetMapping("/page")
    public RestResponse getPage(QueParams queParams, HttpServletRequest request){
        RestResponse response = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize=Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> page=infDeliveryService.getPage(queParams,curPage,pageSize);
            response = RestResponse.succeed(page);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 保存
     *
     * @param infDelivery
     * @return
     */
    @PostMapping("/save")
    public RestResponse save(@RequestBody InfDelivery infDelivery) {
        RestResponse response = null;
        JSONObject jsonObject = new JSONObject();
        try {
            infDeliveryService.save(infDelivery);
            jsonObject.put("message", "保存成功");
            response = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errorMessage = "保存失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 撤回/发布
     *
     * @param queParams
     * @return
     */
    @PostMapping("/recallOrPublish")
    public RestResponse recallOrPublish(@RequestBody QueParams queParams) {
        RestResponse response = null;
        try {
            infDeliveryService.recallOrPublish(queParams);
            if("1".equals(queParams.getReleaseStatus())){
                webSocketManager.broadcastMessage("知识学习发布啦","infDelivery");
            }else{
                webSocketManager.broadcastMessage("知识学习撤回啦","infDelivery");
            }
        } catch (Exception e) {
            String errorMessage="";
            if("1".equals(queParams.getReleaseStatus())){
                errorMessage = "发布失败";
            }else{
                errorMessage = "撤回失败";
            }
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 删除
     * @param
     * @return
     */
    @PostMapping("/delete/{ids}")
    public RestResponse delete(@PathVariable("ids") String ids){
        RestResponse response = null;
        try{
            infDeliveryService.delete(ids);
        }catch (Exception e){
            String errorMessage = "删除失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 查看
     *
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public RestResponse findById(@RequestParam("id") String id) {
        RestResponse response = null;
        try {
            InfDeliveryVo infAchievement=infDeliveryService.findById(id);
            response = RestResponse.succeed(infAchievement);
        } catch (Exception e) {
            String errorMessage = "查看失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 修改
     * @param
     * @return
     */
    @PostMapping("/update")
    public RestResponse update(@RequestBody InfDelivery infDelivery) {
        RestResponse response = null;
        try {
            infDeliveryService.update(infDelivery);
        } catch (Exception e) {
            String errorMessage = "修改失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 目录下载
     * @param
     * @return
     */
    @PostMapping("/catalogDownload/{ids}")
    public void catalogDownload(HttpServletResponse response, @PathVariable("ids") String ids) {
        RestResponse restResponse = null;
        try {
            infDeliveryService.catalogDownload(response, ids);
            //restResponse = RestResponse.succeed("下载成功");
        } catch (Exception e) {
            String errormessage = e.getMessage();
            log.error(errormessage, e);
            //restResponse = RestResponse.fail(errormessage);
        }
        //return restResponse;
    }


    /***
     * 获取迁移数据
     * @param
     * @return
     */
    @GetMapping("/getMoveData")
    public RestResponse getMoveData(QueParams params) {
        RestResponse response = null;
        try {
            List<InfDelivery> dataList = infDeliveryService.getMoveData(params);
            response = RestResponse.succeed(dataList);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 资料分类修改
     * @param
     * @return
     */
    @PostMapping("/meansTypeUpdate")
    public RestResponse meansTypeUpdate(@RequestBody ListVo listvo){
        RestResponse response = null;
        try{
            infDeliveryService.meansTypeUpdate(listvo);
        }catch (Exception e){
            String errorMessage = "修改失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 公众端分页查询
     * @param claCasParams
     * @return
     */
    @GetMapping("/claCasPage")
    @Log(value="知识学习",type = "检索")
    public RestResponse getClaCasPage(ClaCasParams claCasParams, HttpServletRequest request){
        RestResponse response = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize=Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> page=infDeliveryService.getClaCasPage(claCasParams,curPage,pageSize);
            response = RestResponse.succeed(page);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 公众端首页最新资料
     * @return
     */
    @GetMapping("/getLatestInformation")
    public RestResponse getLatestInformation(){
        RestResponse response = null;
        try{
            List<InfDeliveryVo> list=infDeliveryService.getLatestInformation();
            response = RestResponse.succeed(list);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }



}
